<?php

namespace App\Enums;

use BenSampo\Enum\Contracts\LocalizedEnum;
use BenSampo\Enum\Enum;
use Illuminate\Support\Arr;

/**
 * @method static static PRODUCT_IMAGE()
 * @method static static PRODUCT_FILE()
 * @method static static PRODUCT_ENCLOSURE()
 * @method static static AUTH_IMAGE()
 */
final class RegistryFileType extends Enum implements LocalizedEnum
{
    public const PRODUCT_IMAGE = 'product_image';
    public const PRODUCT_FILE = 'product_file';
    public const PRODUCT_ENCLOSURE = 'product_enclosure';
    public const AUTH_IMAGE = 'auth_image';

    public function toArray()
    {
        return $this;
    }

    /** 判断扩展名是否合法 */
    public function isExtAllowed($ext)
    {
        $extMap = [
            self::PRODUCT_FILE => ['png', 'jpg', 'jpeg', 'txt', 'pdf', 'docx', 'mp4', 'mp3', 'wmv', 'wav'],
            self::PRODUCT_IMAGE => ['png', 'jpg', 'jpeg'],
            self::PRODUCT_ENCLOSURE => ['zip', 'rar', '7z', 'jpg', 'jpeg', 'png', 'pdf'],
            self::AUTH_IMAGE => ['png', 'jpg', 'jpeg'],
        ];
        $allowedExts = Arr::get($extMap, $this->value, []);

        return in_array($ext, $allowedExts);
    }

    /** 获取尺寸限制 */
    public function sizeLimit()
    {
        $sizeMap = [
            self::PRODUCT_FILE => 1024 * 1024,          // 1G
            self::PRODUCT_IMAGE => 5 * 1024,            // 5M
            self::PRODUCT_ENCLOSURE => 1024 * 1024,     // 10M
            self::AUTH_IMAGE => 10 * 1024,              // 10M
        ];

        return Arr::get($sizeMap, $this->value);
    }
}
